<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/14
 * Time: 17:39
 */
namespace App\Http\Controllers\Api\V1_6_6;

use App\Http\Requests\Api\V1_6_6\CreditCardUpdateRequest;
use App\Http\Requests\Api\V1_6_6\IdentityImageRequest;
use App\Http\Requests\Api\V1_6_6\IdentityMessageBankCardBindRequest;
use App\Http\Requests\Api\V1_6_6\IdentityMessageCreditCardBindRequest;
use App\Http\Requests\Api\V1_6_6\IdentityMessageRequest;
use App\Http\Requests\Api\V1_6_6\MakeRepaymentBillRecommendDaysRequest;
use App\Http\Requests\Api\V1_6_6\MakeRepaymentBillRequest;
use App\Services\CreditCardService;
use App\Services\RepaymentBillService;
use App\Services\UserPassageService;
use App\Services\Xinyan\IdentityCardService;
use Illuminate\Http\Request;

class RepaymentBillController extends BaseController
{
    protected $repaymentBillService;
    protected $userPassageService;

    public function __construct(RepaymentBillService $repaymentBillService, UserPassageService $userPassageService)
    {
        parent::__construct();
        $this->repaymentBillService = $repaymentBillService;
        $this->userPassageService = $userPassageService;
    }

    /**
     * 还款账单设置
     */
    public function repaymentBillSet(Request $request)
    {
        if (empty($request->id)) {
            return $this->responseJson(422);
        }
        return $this->repaymentBillService->repaymentBillSet($this->user['user_id'], $request->id);
    }

    /**
     * 制定还款账单
     */
    public function makeRepaymentBill(MakeRepaymentBillRequest $request)
    {
        if (180 > $request->repayment_money) {
            return $this->responseJson(10505);//还款金额最低限额180元
        }
        if (80000 < $request->repayment_money) {
            return $this->responseJson(10506);//还款金额最高限额80000元
        }
        $passageId = 1;
        //是否有子商户号
        $userPassage = $this->userPassageService->isHasSubMerchantRegister($this->user, $request->id, $passageId, $request->address);
        if (is_object($userPassage)) return $userPassage;
        return $this->repaymentBillService->makeRepaymentBill($this->user['user_id'], $this->user['level'], $request->id, $request->address,
            $request->repayment_money, $request->repayment_dates, $passageId);
    }

    /**
     * 推荐天数
     * @param MakeRepaymentBillRecommendDaysRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function recommendDays(MakeRepaymentBillRecommendDaysRequest $request)
    {
        if (180 > $request->repayment_money) {
            return $this->responseJson(10505);//还款金额最低限额180元
        }
        if (80000 < $request->repayment_money) {
            return $this->responseJson(10506);//还款金额最高限额80000元
        }
        $passageId = 1;
        return $this->repaymentBillService->recommendDays($this->user['user_id'], $this->user['level'], $request->id, $request->repayment_money, $passageId);
    }

}